#include "aky_program/aky_com_include.h"

static void my_event_cb(lv_obj_t * obj, lv_event_t event);



static lv_obj_t *tc_area_next;          //
static lv_obj_t *tc_area_skip;          //
static lv_obj_t *tc_area_fast_test;          //工程模式入口 连续点击
static uint16_t engineer_click_cnt = 0;
uint8_t g_flag_jump_to_fast_test = 0;

/*
 * 界面回调函数
 */
static void screen002_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    static uint16_t time_cnt = 0;

    static uint16_t engineer_click_cnt_last = 0;

    if (engineer_click_cnt > 0)
    {
        if (engineer_click_cnt_last != engineer_click_cnt)
        {
            engineer_click_cnt_last = engineer_click_cnt;
            time_cnt = 0;
        }
        
        time_cnt++;
        if (time_cnt > 1500 / 10)
        {
            time_cnt = 0;
            engineer_click_cnt = 0;
        }
    }
    else
    {
        time_cnt = 0;
    }

    screen_flush_pro();
    #if 00
        combbine_test();

    #endif

}

static void screen002_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    g_flag_jump_to_fast_test = 0;
    engineer_click_cnt = 0;

    tc_area_next = synwit_ui_find_lv_obj(WIDGET_TOUCH_1);
    tc_area_skip = synwit_ui_find_lv_obj(WIDGET_TOUCH_2);
    lv_obj_set_event_cb(tc_area_next, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_skip, my_event_cb);   /*Assign an event callback*/
    tc_area_fast_test = synwit_ui_find_lv_obj(WIDGET_TOUCH_33);
    lv_obj_set_event_cb(tc_area_fast_test, my_event_cb);   /*Assign an event callback*/

    // lv_obj_t *label_test = lv_label_create(lv_scr_act(), NULL);
    // lv_obj_set_pos(label_test, 0, 0);
    // lv_label_set_long_mode(label_test, LV_LABEL_LONG_BREAK);     /*Break the long lines*/
    // lv_obj_set_width(label_test, 500);
    // lv_obj_set_height(label_test, 50);
    // lv_font_t* font_temp = synwit_ui_font_load("S:spi:wqyMicroHeiMono_28.hmf", false);
    // lv_obj_set_style_local_text_font(label_test,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,font_temp);
    // lv_label_set_text_fmt(label_test, "20%02d年%02d月%02d日   %02d %02d 星期%s", 24, 12, 25, 9, 19,"三");
#if 00
            // voice_enter_bg_play();
            // if (get_voice_busy_stat() == 0)
            {
                Queue_Clear(&qVoice);
            Queue_Push(&qVoice, MEASURE_COMPLET); 
            inner_voice_combine_cmd(INNER_VOICE_PRES_UNIT_MMG, 122, 88,133,66, 77);

                uint8_t who_level = get_bar_level_by_sys_dia(140, 90);


                inner_voice_who(who_level);
                voice_play_array();
            }
            
#endif
#if 00
    g_s_mem.s_time.year = 25;
    g_s_mem.s_time.month = 1;
    g_s_mem.s_time.day = 12;
    insert_one_mem(g_s_mem,g_e_user);

    g_s_mem.s_time.year = 25;
    g_s_mem.s_time.month = 1;
    g_s_mem.s_time.day = 14;

    get_last_mem_gap_day(g_e_user,g_s_mem);
#endif
#if 00
uint8_t j = 4;
    if (g_s_mem_index.cur_mem_index == 0)
    {
        for (uint8_t i = 0; i < 30; i++)
        {
        memset((uint8_t *)&g_s_mem,0,sizeof(s_mem_t));
        g_s_mem.s_time = g_s_time;

            if (j > 0 && i > 10)
            {
                if (i == 14 || i == 15)
                {
                    g_s_mem.s_time.day += j;
                }
                else
                {
                    g_s_mem.s_time.day += j--;
                }
            }
            
            g_s_mem.s_time.secon += i;

            g_s_mem.s_left_bp.sys = 140;
            g_s_mem.s_left_bp.dia = 90;
            g_s_mem.s_left_bp.pul = 20+i;
            g_s_mem.s_right_bp.sys = 20+i;
            g_s_mem.s_right_bp.dia = 10+i;
            g_s_mem.s_right_bp.pul = i;
            g_s_mem.s_train_time.min = i;
            g_s_mem.s_train_time.secon = 10+i;
            insert_one_mem(g_s_mem,USER_1);
            // g_s_mem = flash_get_mem_data(USER_1,g_s_mem_index.cur_mem_index,&g_e_flash_err,MAIN_ADDR);
            AKY_ASM_NOP();
        }
    }   
    get_mem_5_days_high(USER_1);
#endif
    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    screen_init();
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen002_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    screen_stop();

}


static int screen002_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen002_cb_obj = {
    .favorite_stop_policy = screen002_favorite_stop_policy,
    .on_screen_start = screen002_start,
    .on_screen_stop = screen002_stop,
    .on_screen_timer = screen002_on_timer,
};

static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    off_time_clr();
    if (obj == tc_area_next)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN003);
        }
    }
    if (obj == tc_area_skip)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN003);
        }
    }
    if (obj == tc_area_fast_test)
    {
        if (event == LV_EVENT_CLICKED)
        {
            engineer_click_cnt++;
            printf("engineer_click_cnt = %d\r\n", engineer_click_cnt);
            if (engineer_click_cnt >= 5)
            {
                engineer_click_cnt = 0;
                g_flag_jump_to_fast_test = 1;
                synwit_ui_load_screen(SCREEN012);
            }
        }
    }    

}


